#!/bin/bash
# WinsTech 项目一键部署脚本（用于宝塔环境）
# 使用方法: bash deploy.sh

set -e  # 遇到错误立即退出

echo "=================================================="
echo "  WinsTech 项目部署脚本"
echo "=================================================="
echo ""

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 项目路径配置（根据实际情况修改）
PROJECT_ROOT="/www/wwwroot/winstech"
WEB_ROOT="/www/wwwroot/winstech-web"
BACKEND_DIR="$PROJECT_ROOT/backend"
PORTAL_DIR="$PROJECT_ROOT/frontend-portal"
ADMIN_DIR="$PROJECT_ROOT/frontend-admin"

# 检查是否在项目根目录
if [ ! -f "docker-compose.yml" ]; then
    echo -e "${RED}错误：请在项目根目录运行此脚本${NC}"
    exit 1
fi

# 功能菜单
echo "请选择部署选项："
echo "1) 全部部署（后端 + 前端门户 + 管理后台）"
echo "2) 仅部署后端"
echo "3) 仅部署前端门户"
echo "4) 仅部署管理后台"
echo "5) 仅构建前端（不重启服务）"
echo "6) 退出"
echo ""
read -p "请输入选项 [1-6]: " choice

case $choice in
    1)
        DEPLOY_BACKEND=true
        DEPLOY_PORTAL=true
        DEPLOY_ADMIN=true
        ;;
    2)
        DEPLOY_BACKEND=true
        DEPLOY_PORTAL=false
        DEPLOY_ADMIN=false
        ;;
    3)
        DEPLOY_BACKEND=false
        DEPLOY_PORTAL=true
        DEPLOY_ADMIN=false
        ;;
    4)
        DEPLOY_BACKEND=false
        DEPLOY_PORTAL=false
        DEPLOY_ADMIN=true
        ;;
    5)
        DEPLOY_BACKEND=false
        DEPLOY_PORTAL=true
        DEPLOY_ADMIN=true
        BUILD_ONLY=true
        ;;
    6)
        echo "退出部署"
        exit 0
        ;;
    *)
        echo -e "${RED}无效选项${NC}"
        exit 1
        ;;
esac

echo ""
echo "=================================================="

# 后端部署
if [ "$DEPLOY_BACKEND" = true ]; then
    echo ""
    echo -e "${GREEN}[1/3] 部署后端...${NC}"
    echo ""
    
    cd $BACKEND_DIR
    
    # 激活虚拟环境
    if [ ! -d "venv" ]; then
        echo "创建Python虚拟环境..."
        python3 -m venv venv
    fi
    
    source venv/bin/activate
    
    # 安装/更新依赖
    echo "安装Python依赖..."
    pip install --upgrade pip -q
    pip install -r requirements.txt -q
    
    # 检查是否安装oss2
    if ! pip show oss2 > /dev/null 2>&1; then
        echo "安装阿里云OSS SDK..."
        pip install oss2 -q
    fi
    
    # 创建必要目录
    mkdir -p logs static/uploads
    
    # 重启后端服务
    echo "重启后端服务..."
    
    # 尝试使用supervisorctl重启
    if command -v supervisorctl &> /dev/null; then
        if supervisorctl status winstech-backend &> /dev/null; then
            supervisorctl restart winstech-backend
            echo -e "${GREEN}✓ 后端服务已通过Supervisor重启${NC}"
        else
            echo -e "${YELLOW}! Supervisor未配置winstech-backend，请手动重启${NC}"
        fi
    else
        echo -e "${YELLOW}! 请手动重启后端服务（使用宝塔Python项目管理器或PM2）${NC}"
    fi
    
    deactivate
    
    echo -e "${GREEN}✓ 后端部署完成${NC}"
fi

# 前端门户部署
if [ "$DEPLOY_PORTAL" = true ]; then
    echo ""
    echo -e "${GREEN}[2/3] 部署前端门户...${NC}"
    echo ""
    
    cd $PORTAL_DIR
    
    # 检查是否有.env.production
    if [ ! -f ".env.production" ]; then
        echo -e "${YELLOW}警告：.env.production 不存在，将使用默认配置${NC}"
    fi
    
    # 安装依赖
    echo "安装npm依赖..."
    npm install --registry=https://registry.npmmirror.com
    
    # 构建
    echo "构建前端门户..."
    npm run build
    
    if [ "$BUILD_ONLY" != true ]; then
        # 部署到Nginx目录
        echo "部署到Nginx目录..."
        mkdir -p $WEB_ROOT/portal
        rm -rf $WEB_ROOT/portal/*
        cp -r dist/* $WEB_ROOT/portal/
        
        echo -e "${GREEN}✓ 前端门户部署完成${NC}"
    else
        echo -e "${GREEN}✓ 前端门户构建完成（未部署）${NC}"
    fi
fi

# 管理后台部署
if [ "$DEPLOY_ADMIN" = true ]; then
    echo ""
    echo -e "${GREEN}[3/3] 部署管理后台...${NC}"
    echo ""
    
    cd $ADMIN_DIR
    
    # 检查是否有.env.production
    if [ ! -f ".env.production" ]; then
        echo -e "${YELLOW}警告：.env.production 不存在，将使用默认配置${NC}"
    fi
    
    # 安装依赖
    echo "安装npm依赖..."
    npm install --registry=https://registry.npmmirror.com
    
    # 构建
    echo "构建管理后台..."
    npm run build
    
    if [ "$BUILD_ONLY" != true ]; then
        # 部署到Nginx目录
        echo "部署到Nginx目录..."
        mkdir -p $WEB_ROOT/admin
        rm -rf $WEB_ROOT/admin/*
        cp -r dist/* $WEB_ROOT/admin/
        
        echo -e "${GREEN}✓ 管理后台部署完成${NC}"
    else
        echo -e "${GREEN}✓ 管理后台构建完成（未部署）${NC}"
    fi
fi

# 重载Nginx
if [ "$BUILD_ONLY" != true ] && ([ "$DEPLOY_PORTAL" = true ] || [ "$DEPLOY_ADMIN" = true ]); then
    echo ""
    echo "重载Nginx配置..."
    if command -v nginx &> /dev/null; then
        nginx -t && nginx -s reload
        echo -e "${GREEN}✓ Nginx配置已重载${NC}"
    else
        echo -e "${YELLOW}! 请手动重载Nginx配置${NC}"
    fi
fi

echo ""
echo "=================================================="
echo -e "${GREEN}部署完成！${NC}"
echo "=================================================="
echo ""
echo "访问地址："
echo "  - 官网前端: http://154.8.224.221"
echo "  - 管理后台: http://154.8.224.221/admin"
echo "  - 后端API:  http://154.8.224.221/api/docs"
echo ""
echo "提示："
echo "  - 如需查看后端日志: tail -f $BACKEND_DIR/logs/app.log"
echo "  - 如需查看Nginx日志: tail -f $WEB_ROOT/logs/error.log"
echo ""

